/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principals;

import java.awt.Event;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.InputMap;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

/**
 *
 * @author Hans
 */
public class Validadores {

    private Pattern patternEMAIL;
    private Pattern patternCaracteres;
    private Pattern patternCaracteresEmail;
    private Pattern patternCaracteresDireccion;
    private Matcher matcher;
    private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    private static final String CARACTERES_RAROS = "[^'$#%&/·;¡!:°|>'<.,^#()+{}¿*?_=~`@\"\\\\]";
    private static final String CARACTERES_EMAIL = "[^'$#%&/·;¡!:°|>'<,^#()+{}¿*?=~`\"]";
    private static final String CARACTERES_DIREC = "[^'$%&/·;¡!:|><^()+{}¿*?_=~`@]";

    public Validadores() {
        patternEMAIL = Pattern.compile(EMAIL_PATTERN);
        patternCaracteres = Pattern.compile(CARACTERES_RAROS);
        patternCaracteresEmail = Pattern.compile(CARACTERES_EMAIL);
        patternCaracteresDireccion = Pattern.compile(CARACTERES_DIREC);
    }

    public int validaOcurrencias(char cBuscado, String sBuscar) {
        int count = 0;
        for (int i = 0; i < sBuscar.length(); i++) {
            if (sBuscar.charAt(i) == cBuscado) {
                count++;
            }
        }
        return count;
    }

    public void validaCaracteresRaros(java.awt.event.KeyEvent evt) {
        char c = evt.getKeyChar();
        matcher = patternCaracteres.matcher(String.valueOf(c));
        if (matcher.matches()) {
        } else {
            evt.consume();
        }
    }

    public void validaCaracteresEmail(java.awt.event.KeyEvent evt) {
        char c = evt.getKeyChar();
        matcher = patternCaracteresEmail.matcher(String.valueOf(c));
        if (matcher.matches()) {
        } else {
            evt.consume();
        }
    }

    public void validaCaracteresDirec(java.awt.event.KeyEvent evt) {
        char c = evt.getKeyChar();
        matcher = patternCaracteresDireccion.matcher(String.valueOf(c));
        if (matcher.matches()) {
        } else {
            evt.consume();
        }
    }

    public void validaMaxSize(java.awt.event.KeyEvent evt, int max, String value) {
        if (value.length() == max) {
            evt.consume();
        }
    }

    public boolean validaEmail(final String hex) {
        matcher = patternEMAIL.matcher(hex);
        return matcher.matches();
    }

    public void validaSoloNumeros(java.awt.event.KeyEvent evt) {
        char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != '\b' /*corresponde a BACK_SPACE*/)) {
            evt.consume();  // ignorar el evento de teclado
        }
    }

    public void validaDoublePositivo(java.awt.event.KeyEvent evt) {
        char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if (((caracter < '0') || (caracter > '9')) && (caracter != '\b' /*corresponde a BACK_SPACE*/) && (caracter != '.')) {
            evt.consume();  // ignorar el evento de teclado
        }
    }

    public void validaDouble(java.awt.event.KeyEvent evt) {
        char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if (((caracter < '0') || (caracter > '9')) && (caracter != '\b' /*corresponde a BACK_SPACE*/) && (caracter != '.') && (caracter != '-')) {
            evt.consume();  // ignorar el evento de teclado
        }
    }

    public void validaTelefono(java.awt.event.KeyEvent evt) {
        char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if (((caracter < '0') || (caracter > '9'))
                && (caracter != '\b' /*corresponde a BACK_SPACE*/) && (caracter != '-')) {
            evt.consume();  // ignorar el evento de teclado
        }
    }

    public void validaSoloText(java.awt.event.KeyEvent evt) {
        char c = evt.getKeyChar();
        if (Character.isDigit(c) == false) {
        } else {
            evt.consume();
        }
    }

    public void validaSoloNumerosLetras(java.awt.event.KeyEvent evt) {
        char c = evt.getKeyChar();
        if (Character.isDigit(c) || Character.isLetter(c)) {
        } else {
            evt.consume();
        }
    }

    public void validaPlaca(java.awt.event.KeyEvent evt) {
        char c = evt.getKeyChar();
        if (Character.isDigit(c) || Character.isLetter(c) || c == '-') {
        } else {
            evt.consume();
        }
    }

    public void desactivarAccionPegar(JTextField caja) {
        InputMap map = caja.getInputMap();
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK), "null");
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, Event.SHIFT_MASK), "null");
    }

    public void disableCopyAction(JTextField caja) {
        InputMap map = caja.getInputMap();
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK), "null");
    }

    public void desactivarAccionPegar(JTextArea area) {
        InputMap map = area.getInputMap();
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK), "null");
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, Event.SHIFT_MASK), "null");
    }
    
    public void desactivarAccionPegar(JPasswordField pwd) {
        InputMap map = pwd.getInputMap();
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK), "null");
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, Event.SHIFT_MASK), "null");
    }
}
